﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.Drawing.Imaging;

using LowLevelGraphics;
using LowLevelGraphics.Filter;

namespace ImageRecognition2.Forms
{
    /// <summary>
    /// This could be added to form.
    /// This will show a small version of the original pic
    /// And this component can be used to show the impact of the filter
    /// </summary>
    public class FilterPreviewControl : Control
    {
        protected Bitmap m_Bitmap = null;
        protected Bitmap m_ScaledBitmap = null;
        protected IFilter m_IFilter = null;

        /// <summary>
        /// Dfault constructor
        /// </summary>
        public FilterPreviewControl()
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="FilterPreviewControl"/> class.
        /// </summary>
        /// <param name="_filter">The _filter.</param>
        public FilterPreviewControl(IFilter _filter)
            : this(null, _filter)
        {

        }

        /// <summary>
        /// Initializes a new instance of the <see cref="FilterPreviewControl"/> class.
        /// </summary>
        /// <param name="_bitmap">The _bitmap.</param>
        public FilterPreviewControl(Bitmap _bitmap)
            : this(_bitmap, null)
        {
        }

        /// <summary>
        /// Specify bitmap and filter directly
        /// </summary>
        /// <param name="_bitmap"></param>
        /// <param name="_filter"></param>
        public FilterPreviewControl(Bitmap _bitmap, IFilter _filter)
        {
            m_Bitmap = _bitmap;
            m_IFilter = _filter;
        }

        /// <summary>
        /// Causes the control to redraw the invalidated regions within its client area.
        /// </summary>
        /// <PermissionSet>
        /// 	<IPermission class="System.Security.Permissions.EnvironmentPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true"/>
        /// 	<IPermission class="System.Security.Permissions.FileIOPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true"/>
        /// 	<IPermission class="System.Security.Permissions.SecurityPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Flags="UnmanagedCode, ControlEvidence"/>
        /// 	<IPermission class="System.Diagnostics.PerformanceCounterPermission, System, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true"/>
        /// </PermissionSet>
        public void Update()
        {
            m_ScaledBitmap = new Bitmap(m_Bitmap, this.Size.Width, this.Size.Height);
            m_IFilter.Execute(m_ScaledBitmap);
        }

        /// <summary>
        /// Raises the <see cref="E:System.Windows.Forms.Control.Paint"/> event.
        /// </summary>
        /// <param name="e">A <see cref="T:System.Windows.Forms.PaintEventArgs"/> that contains the event data.</param>
        protected override void OnPaint(PaintEventArgs e)
        {
            e.Graphics.DrawImage(m_ScaledBitmap, this.ClientRectangle, this.ClientRectangle.Location.X, this.ClientRectangle.Location.Y, this.ClientRectangle.Width, this.ClientRectangle.Height, GraphicsUnit.Display);
        }

        /// <summary>
        /// Gets or sets the bitmap
        /// </summary>
        public Bitmap Bitmap
        {
            get { return m_Bitmap; }
            set { m_Bitmap = value; }
        }

        /// <summary>
        /// Gets or sets a filter
        /// </summary>
        public IFilter Filter
        {
            get { return m_IFilter; }
            set { m_IFilter = value; }
        }
    }
}
